package org.xg.study.servlet;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author CMG
 * @version 1.0
 * @description
 * @cLassName MyServlet
 * @date 2021/9/23 10:02
 **/
public class MyServlet implements Servlet {

    /**
     * Servlet实例被创建后，调用init()方法进行初始化，该方法只能被调用一次
     * @param servletConfig
     * @throws ServletException
     */
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    /**
     * 返回ServletConfig对象，该对象包含了Servlet的初始化参数
     * @return
     */
    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    /**
     * 每次请求，都会调用一次service()方法
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //设置字符集
        servletResponse.setContentType("text/html;charset=UTF-8");
        //使用PrintWriter.write()方法向前台输出内容
        PrintWriter printWriter = servletResponse.getWriter();
        printWriter.write("Hello World");
        printWriter.close();
    }

    /**
     * 返回关于Servlet的信息，例如作者、版本、版权等
     * @return
     */
    @Override
    public String getServletInfo() {
        return null;
    }

    /**
     * Servlet被销毁时调用
     */
    @Override
    public void destroy() {

    }
}
